(function (window, $) {
  var normal = TianMap.getNormal();

  var CellMap = {
    init: function () {
      var me = this;
      me.render();
      me.bind();
    },
    url: {
      cell: Page.ctx + '/ai/sceResultForTest/cellMap',
      cellInfo: Page.ctxStatic + '/mock/json/cellInfo.json'
    },
    layer: {
      canvas: L.canvasLayer(),
      normal: normal,
      overlay: {},
      baseLayer: { // 切换基础图层的对象
        "地图": normal,
        "影像": TianMap.getImage(),
        "地形": TianMap.getTerrain()
      },
    },
    data: {
      points: []
    },
    render: function () {
      var me = this;
      me.map = me.initMap('cellMap');
    },
    bind: function () {
      var me = this;
      var canvas = me.layer.canvas;
      var map = me.map;

      // 拖动事件
      map.on('dragstart', function (e) {
        if (map.hasLayer(canvas)) {
          canvas.delegate(window).removeFrom(map);
        }
      });

      // 拖动结束事件
      map.on('dragend', function (e) {
        if (!map.hasLayer(canvas)) {
          canvas.delegate(window).addTo(map);
        }
      });
    },
    setMapData: function (data) {
      var me = this;
      var map = me.map;
      var length = data.length;

      if (length === 0) {
        me.setMapNoData();
      } else {
        me.data.points = data;

        var center = PointUtil.getCenter(me.data.points);

        me.layer.canvas.delegate(window).addTo(me.map);

        // 定位地图
        map.setView({
          lat: center.lat,
          lng: center.lng
        }, 10);
      }
    },
    setMapNoData: function () {
      var me = this;
      me.map.setView({
        lng: 107.90,
        lat: 35.84
      }, 4);
    },
    initMap: function (selId) {
      var me = this;

      var cmap = new CMap(selId, me.layer.normal);
      var map = cmap.getMap();
      cmap.setLayers(this.layer.baseLayer, this.layer.overlay).addZoom().addCoord().addScale();
      map.setView({
        lng: 107.90,
        lat: 35.84
      }, 4);

      return map;
    }
  };

  CellMap.init();
  window.CellMap = CellMap;
})(window, jQuery);

/**
 * gLayers.Leaflet插件的回调函数
 *
 * @param info
 * @return
 */
var onDrawLayer = function (info) {
  // 地图和createjs的stage
  var map = info.layer._map,
    mapContainer = map._container.id,
    stage = new createjs.Stage(info.canvas),
    zoomLevel = map.getZoom(),
    sectorRadius = SectorAngle.getRadius(zoomLevel);

  // 基站和扇区数据
  var points = mapContainer === 'cellMap' ? CellMap.data.points : BestCellMap.data.points,
    requestUrl = mapContainer === 'cellMap' ? CellMap.url.cellInfo : CellMap.url.cellInfo,
    length = points.length;

  points.reverse();

  stage.enableMouseOver(20);
  stage.clear();

  for (var i = 0; i < length; i++) {
    if (info.bounds.contains({lat: points[i].lat, lon: points[i].lon})) {
      // 如果点的经纬度在地图范围内
      // 将地图经纬度坐标转换为页面内的容器坐标
      var dot = map.latLngToContainerPoint({lat: points[i].lat, lon: points[i].lon});
      var sector = points[i].ana;

      if (points[i].checked === true) {
        var fillStyle = 'rgba(255, 0, 0, 0.8)';
      } else {
        var fillStyle = 'rgba(244, 225, 198, 0.6)';
      }

      Draw.sector({
        stage: stage,
        x: dot.x,
        y: dot.y,
        start: SectorAngle.start(sector),
        end: SectorAngle.end(sector),
        radius: sectorRadius,
        fillStyle: fillStyle,
        strokeStyle: 'rgb(136, 123, 39)',
        anticlock: true,
        event: 'click',
        requestUrl: requestUrl,
        // requestData: {
        //   cellId: points[i].id
        // },
        info: {
          cgi: points[i].cgi,
          ana: points[i].ana,
          lat: points[i].lat,
          lon: points[i].lon
        },
        map: map,
        points: points,
      });
    }
  }

  stage.update();
}